package com.example.bensonzheng.appcompattest;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

public class BaseAppCompatAcitvity extends AppCompatActivity {
    public Toolbar mToolbar;
    public ViewGroup mFrameView;
    private View mStatusView;
    private static String TAG;
    protected Drawable mToolbarDrawable;
    private boolean isUseCustomStatusView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TAG = getClass().getName().toString();

        isUseCustomStatusView = isUseCustomStatusView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            getWindow().setAllowEnterTransitionOverlap(true);
            getWindow().setAllowReturnTransitionOverlap(true);
        }

        super.onCreate(savedInstanceState);
        initFrameView();
        initToolBar();
        initCustomStatusView();

    }

    protected void initCustomStatusView() {
        int color = getResources().getColor(R.color.colorPrimaryDark);
        Utils.setMaterialDesignMode(this, isUseCustomStatusView, color);
        if (isUseCustomStatusView && Build.VERSION.SDK_INT >= 19) {
            mStatusView = findViewById(R.id.statusView);
            if (mStatusView != null) {
                mStatusView.getLayoutParams().height = Utils.getStatusBarHeight(this);
                mStatusView.setBackgroundResource(R.color.colorPrimaryDark);
            }
        }
    }

    protected void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbarDrawable=getResources().getDrawable(R.drawable.bg_card);
            getSupportActionBar().setBackgroundDrawable(mToolbarDrawable);

        }
    }

    /**
     * 加载带ToolBar布局框架
     */
    protected void initFrameView() {

        int id = provideFrameLayoutResId();
        if (id > 0) {
            mFrameView = (ViewGroup) View.inflate(this, id, null);
            ViewGroup content = (ViewGroup) findViewById(android.R.id.content);
            ViewGroup contentParent = (ViewGroup) content.getParent();
            contentParent.removeView(content);//先把contentView从父容器移除掉
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            contentParent.addView(mFrameView, layoutParams);//加载自己的框架容器
            View contentPlaceholderView = mFrameView.findViewById(R.id.content_placeholder);
            //contentPlaceholderView是占位view,先用它做布局，到这里再把替换成contentView
            if (contentPlaceholderView != null) {
                ViewGroup.LayoutParams contentLayoutParams = contentPlaceholderView.getLayoutParams();
                mFrameView.removeView(contentPlaceholderView);
                mFrameView.addView(content, 0, contentLayoutParams);
                return;
            }
            mFrameView.addView(content);
        }
    }

    /**
     * 获取框架布局的资源id
     *
     * @return
     */
    protected int provideFrameLayoutResId() {
        return R.layout.base_frame_layout;
    }

    /**
     * 添加自定义控件到ToolBar
     * 注意：mToolbar不能为空，默认靠右居中
     *
     * @param view
     */
    protected void addCustomViewToToolBar(View view) {
        addCustomViewToToolBar(view, Gravity.RIGHT);
    }

    /**
     * 添加自定义控件到ToolBar
     * 注意：mToolbar不能为空
     *
     * @param view
     * @param gravity 居中方式
     */
    protected void addCustomViewToToolBar(View view, int gravity) {
        if (mToolbar == null) {
            Log.e(TAG, "toolbar must no null");
            return;
        }
        Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, gravity);
        mToolbar.addView(view, layoutParams);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_base_app_compat_acitvity, menu);
        return true;
    }

    /**
     * 获得自定义的状态栏，需要重写isUseCustomStatusView 返回true
     *
     * @return
     */
    public View getCustomStatusView() {
        return mStatusView;
    }

    /**
     * 使用自定义的状态栏
     * 注：当返回true使用自定义的状态栏，可用控制状态栏的透明度。
     * 需要开启此功能，重写此方法既可
     *
     * @return
     */
    public boolean isUseCustomStatusView() {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        } else if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
